//@charset "utf-8";
// 默认主体
define(function(require, exports, module) {
	var Backbone = require('backbone');

	var hotQueryModel = require('../models/hotquery');
	var historyQueryModel = require('../models/historyquery');
	var plateModel = require('../models/platelist');
	var BaseView = require('./base');
	var baseView = new BaseView();
	var CodeListModel = require('../models/codelist');
	var plateListModel = require('../models/platelist');

	var ContentView = Backbone.View.extend({
		el: '.contentbox',
		hotquery: '',
		initialize: function() {
			this.init();
		},
		init: function() {
			this.renderHotQuery();
			$('#noResultView').hide();
			$('#resultStockView').hide();
			$('#view_result_condition').hide();
			if(baseView.getQuery('plateid')){
				$("#defaultView").hide();
				return;
			}
			$("#defaultView").show();
		},
		events: {
			'click .view_lastsearch': 'doSearch',
			'click .view_hotquery': 'doHotSearch',
			'click #view_clearbtn': 'deleteSearchContent',
			'click #view_searchinput': 'showLastSearch',
			'keyup #view_searchinput': 'showDeleteSearchBnt',
			'click #view_select_choose': 'selectChoosePlate',
			'click .view_div_option_plate': 'selectOptionPlate',
			'mouseenter .view_div_option_plate': 'enterOptionPlate',
			'mouseleave .view_div_option_plate': 'leaveOptionPlate'
		},
		showLastSearch: function(e) {
			if ($('#view_searchinput').val() == '') {
				this.renderHistoryQuery();
				$('#view_search_options_relation').show();
			}
			$(document).one("click", function() {
				$("#view_search_options_relation").hide();
			});
			e.stopPropagation();
		},
		showDeleteSearchBnt: function() {
			if ($('#view_searchinput').val() == '') {
				$('#view_search_options_relation').show();
				$('#view_clearbtn').hide();
				$('#operate').hide();
			} else {
				$('#view_clearbtn').show();
			}
		},
		renderHistoryQuery: function() {
			var self = this;
			var html = [];
			var queryList = historyQueryModel.getData();
			//如果不存在历史搜索，则显示火热推荐
			if (false === queryList) {
				queryList = self.hotquery;
				for (var i in queryList) {
					html.push('<p class="view_hotquery" title="' + queryList[i]['query_title'] + '">' + queryList[i]['show_title'] + '<span>Hot</span></p>');
				}
			} else {
				for (var i in queryList) {
					html.push('<a href="###" view_click_stat="cop_58120aef_245" class="view_lastsearch" title="' + queryList[i] + '">' + queryList[i] + '</a>');
				}
			}

			$('#view_search_options_relation').html(html.join(''));

			$('#view_search_options_relation').show();
		},
		renderHotQuery: function() {
			var self = this;
			hotQueryModel.callback = function(data) {
				self.hotquery = data;
				var updateTime = '--';
				updateTime = data[0]['update_time'] || '';
				var dom = '<p class="recommend-title">关注度最高的板块<span class="recommend-time">' + updateTime + '</span></p>';
				$.each(data, function(i, item) {
					var profit = item.profit == undefined ? '--' : item.profit;
					dom += '<div class="recommend-div view_hotquery" title="' + item.query_title + '"><span class="recommend-zf">（今日涨幅&nbsp;<span>' + profit + '%</span>）</span><span class="recommend-link">' + item.query_title + '</span></div>'
				});
				$('#hotbox').html(dom);
			};

			hotQueryModel.request();
		},
		doHotSearch: function(e) {
			TA.log({
				id: '579098b2_977'
			});
			this.doSearch(e);
		},
		doSearch: function(e) {
			var self = this;
			e = e || window.event;
			var str = $(e.currentTarget).attr('title');
			$('#view_searchinput').val(str);
			$('#view_searchbtn').click();
			self.showDeleteSearchBnt();
		},
		deleteSearchContent: function() {
			$('#view_searchinput').val('');
			$("#view_select_option").hide();
			$('#view_select_option_container').hide();
			//$('#view_search_options_relation').show();
			$("#defaultView").show();
			$('#operate').hide();
			$('#resultStockView').hide();
			$('#view_result_condition').hide();
			$('#view_clearbtn').hide();
			$('#noResultView').hide();
			$('#view_search_options_relation').hide();

			//COCO新增--当有plateid时--》点击叉叉出现昨日前十板块
			if(baseView.getQuery('plateid')){
				$("#defaultView").hide();
				$('#tenstock').show();
			}

		},
		renderPlateOption: function() {
			plateModel.requestList(function(data) {
				var dom = '';
				// alert(JSON.stringify(data))
				for (var i in data) {
					if (data[i]['isSelect'] === 1) {
						continue;
					}
					dom += '<div class="option view_div_option_plate" value="' + data[i]['id'] + '">' + data[i]['name'] + '</div>';
				}
				$("#view_select_option").html(dom);
				$('#view_select_option').show();
				$('#view_select_option_container').show();
			});

		},
		selectChoosePlate: function(e) {
			var self = this;
			e = e || window.event;
			var $dom = $(e.currentTarget);
			if ($("#view_select_choose_name").hasClass("add-plate-lock")) {
				return;
			}
			if (!$("#view_select_option").is(':hidden')) {
				$("#view_select_option").hide();
				$("#view_select_option_container").hide();
				return;
			}

			$(document).on("click", function() {
				$("#view_select_option").hide();
				$("#view_select_option_container").hide();
			});
			self.renderPlateOption();
			e.stopPropagation();
		},
		selectOptionPlate: function(e) {
			e = e || window.event;
			var $dom = $(e.currentTarget);
			var plateId = $dom.attr("value");
			var plateName = $dom.html();
			//$('#view_select_choose_name').html(plateName);
			CodeListModel.coverCodeList();
			var codelist = CodeListModel.getCodeListStr();
			if (codelist.length <= 0) {
				baseView.showtip('请选择股票');
				return;
			}
			baseView.savePlateAdd(plateId, codelist, $("#items-data-id-" + plateId));
			TA.log({
				id: "cop_581207d8_589"
			});
		},
		enterOptionPlate: function(e) {
			e = e || window.event;
			var $dom = $(e.currentTarget);
			$dom.addClass('selectEnter');
		},
		leaveOptionPlate: function(e) {
			e = e || window.event;
			var $dom = $(e.currentTarget);
			$dom.removeClass('selectEnter');
		}
	});

	module.exports = new ContentView;
});
